package daily.may0509;

class MyGenericClass<TYPE>{
    //TYPE在这里代表一种未知数据类型，届时传递什么就是什么类型
    private TYPE name;
    //getter和setter方法
    public TYPE getName() {
        return name;
    }
    public void setName(TYPE name) {
        this.name = name;
    }
}
public class GenericClassTest {
    public static void main(String[] args) {
        //不写泛型默认为Object类型
        MyGenericClass myGenericClass_1 = new MyGenericClass();
        myGenericClass_1.setName("不指定泛型则默认为Object类型");
        Object name_1 = myGenericClass_1.getName();
        System.out.println(name_1);//不指定泛型则默认为Object类型
        //创建MyGenericClass对象，泛型使用Integer类型
        MyGenericClass<Integer> myGenericClass_2 = new MyGenericClass<>();
        myGenericClass_2.setName(123);
        Integer name_2 = myGenericClass_2.getName();
        System.out.println(name_2);//123
        //创建MyGenericClass对象，泛型使用String类型
        MyGenericClass<String> myGenericClass_3 = new MyGenericClass<>();
        myGenericClass_3.setName("只能为字符串");
        String name_3 = myGenericClass_3.getName();
        System.out.println(name_3);//只能为字符串
    }
}
